package com.remote.ui.controller;

import com.remote.common.context.listener.ReadListener;
import com.remote.ui.core.Controller;
import com.remote.ui.core.ControllerManager;
import com.remote.ui.core.ControllerStarter;
import io.netty.channel.ChannelHandlerContext;
import javafx.application.Platform;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;

public abstract class BaseController implements Controller, Initializable, ReadListener {

    private ControllerManager controllerManager;
    private Map<String, Object> vars;
    private Scene scene;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @Override
    public void onRead(ChannelHandlerContext ctx, Object obj) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                onReadDate(ctx, obj);
            }
        });
    }

    @Override
    public void onCreate(ControllerManager controllerManager, Scene scene, Map<String, Object> vars) {
        this.controllerManager = controllerManager;
        this.scene = scene;
        this.vars = vars;
        this.initView(scene, vars);
        this.initEvent(scene, vars);
    }

    protected abstract void initView(Scene scene, Map<String, Object> vars);

    protected abstract void initEvent(Scene scene, Map<String, Object> vars);

    protected abstract void onReadDate(ChannelHandlerContext ctx, Object obj);

    /**
     * 获取上一个界面传的值
     *
     * @param name
     * @return
     */
    @Override
    public Object getValue(String name) {
        return vars.get(name);
    }

    /**
     * 弹出对话框
     *
     * @param message
     */
    protected void showMessage(String message) {
        showMessage("提示", message);
    }

    /**
     * 弹出对话框
     *
     * @param title
     * @param message
     */
    @Override
    public void showMessage(String title, String message) {
        Stage window = new Stage();
        window.setTitle(title);
        //modality要使用Modality.APPLICATION_MODEL
        window.initModality(Modality.APPLICATION_MODAL);
        window.setMinWidth(300);
        window.setMinHeight(150);

        Button button = new Button("关闭");
        button.setOnAction(e -> window.close());

        Label label = new Label(message);

        VBox layout = new VBox(10);
        layout.getChildren().addAll(label, button);
        layout.setAlignment(Pos.CENTER);

        Scene scene = new Scene(layout);
        window.setScene(scene);
        //使用showAndWait()先处理这个窗口，而如果不处理，main中的那个窗口不能响应
        window.showAndWait();
    }

    @Override
    public ControllerManager getControllerManager() {
        return controllerManager;
    }

    @Override
    public ControllerStarter getStarter(String fxml) {
        return controllerManager.getStarter(fxml);
    }

    @Override
    public Scene getScene() {
        return scene;
    }
}
